package com.bw.zg3park2212.point

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.PointEntity
import com.bw.base.entity.PointEntityItem
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.adpter.OrderAdpter
import com.bw.zg3park2212.adpter.PointAdpter
import com.bw.zg3park2212.databinding.ActivityPointBinding
import com.bw.zg3park2212.point.PointViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/PointActivity")
class PointActivity : BaseActivity<ActivityPointBinding,PointViewModel>() {
    lateinit var pointAdpter: PointAdpter
     var list= mutableListOf<PointEntityItem>()
    var types=""
    override fun intitView() {
        pointAdpter= PointAdpter()
        binding.rvPatrol.adapter=pointAdpter
        binding.rvPatrol.layoutManager=LinearLayoutManager(this)
        var arr= mutableListOf("保洁巡检","安防巡检","日常巡检","家居巡检")
        var typeAdpter=ArrayAdapter(this,android.R.layout.simple_list_item_1,arr)
        binding.patrolStatus.adapter=typeAdpter
        typeAdpter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.patrolStatus.onItemSelectedListener= object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long,
            ) {
                types=arr[position]
                pointAdpter.data.clear()
                pointAdpter.addData(list.filter { it.patrolType==types})
                pointAdpter.notifyDataSetChanged()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }
        binding.patrolSv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query!=null){
                    viewmodel.getpointsearch(query,types)
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                viewmodel.getpoint()
                return false
            }
        })
        binding.keep.setOnClickListener {
            ARouter.getInstance().build("/app/PointAddActivity").navigation()
        }
        pointAdpter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/PointXiangActivity")
                .withSerializable("pointid",pointAdpter.data[position])
                .navigation()
        }
        pointAdpter.setOnItemLongClickListener { adapter, view, position ->
            viewmodel.getpointdelete(pointAdpter.data[position].id)
            list.removeAt(position)
            pointAdpter.remove(position)
            return@setOnItemLongClickListener true
        }
    }

    override fun intitData() {
        viewmodel.pointSuccse.observe(this){
            pointAdpter.data.clear()
            list.clear()
            list.addAll(it)
            pointAdpter.addData(list.filter { it.patrolType=="保洁巡检" })
            pointAdpter.notifyDataSetChanged()
        }
        viewmodel.pointsearchSuccse.observe(this){
            pointAdpter.data.clear()
            pointAdpter.addData(it)
            pointAdpter.notifyDataSetChanged()
            ToastUtils.showLong("查询成功")
        }
        viewmodel.pointdeleteSuccse.observe(this){
            ToastUtils.showLong("删除成功")
        }
    }

    override fun onResume() {
        super.onResume()
        viewmodel.getpoint()
    }
}